import 'package:flutter/material.dart';
import 'package:flutter_account_book/generated/l10n.dart';

class PickItem {
  int value = 0;
  String label = "";
  Image? image;
  dynamic obj;
  PickItem({required this.value, required this.label, this.image, this.obj});
  @override
  int get hashCode => value.hashCode;
}

class PickList extends StatefulWidget {
  final List<PickItem> items;
  final String title;
  final int? defalutValue;
  const PickList(
      {super.key,
      this.title = "Select Topics",
      this.defalutValue,
      required this.items});
  @override
  State<StatefulWidget> createState() => _PickList();
}

class _PickList extends State<PickList> {
  int? _selectedValue;

  @override
  void initState() {
    super.initState();
    _selectedValue = widget.defalutValue;
  }

  @override
  Widget build(BuildContext context) {
    return AlertDialog(
        title: Text(widget.title),
        content: Container(
            width: 200,
            height: 150,
            child: ListView.builder(
              itemCount: widget.items.length,
              itemBuilder: (BuildContext context, int index) {
                return ListTile(
                  title: Text(widget.items[index].label),
                  leading: widget.items[index].image,
                  trailing: Radio<int>(
                    value: widget.items[index].value,
                    groupValue: _selectedValue,
                    onChanged: (int? value) {
                      setState(() {
                        _selectedValue = value;
                      });
                      Navigator.of(context).pop(widget.items[index]);
                    },
                  ),
                  onTap: () {
                    setState(() {
                      _selectedValue = widget.items[index].value;
                    });
                    Navigator.of(context).pop(widget.items[index]);
                  },
                );
              },
            )),
        actions: [
          TextButton(
            onPressed: () {
              Navigator.pop(context);
            },
            child: Text(S.current.cancel),
          ),
        ]);
  }
}
